/**
 * @description 系统路由配置
 * @author 滕轶男
 */

import React, { lazy } from 'react'
import { createBrowserRouter } from 'react-router-dom'
import MainLayout from '../layouts/MainLayout'
import ManageLayout from '../layouts/ManageLayout'
import QuestionLayout from '../layouts/QuestionLayout'
import Home from '../pages/Home'
import Login from '../pages/Login/Login'
import Register from '../pages/Register/Register'
import Notfound from '../pages/Notfound'
import List from '../pages/manage/List'
import Trash from '../pages/manage/Trash'
import Star from '../pages/manage/Star'
// import Edit from '../pages/question/Edit'
// import Statistics from '../pages/question/Statistics'
// 使用路由懒加载，拆分 bundle，优化首页体积
const Edit = lazy(() => import(/* webpackChunkName: 'EditPage' */ '../pages/question/Edit'))
const Statistics = lazy(
    () => import(/* webpackChunkName: 'StatisticsPage' */ '../pages/question/Statistics')
)

const router = createBrowserRouter([
    {
        path: '/',
        element: <MainLayout />,
        children: [
            {
                path: '/',
                element: <Home />,
            },
            {
                path: 'login',
                element: <Login />,
            },
            {
                path: 'register',
                element: <Register />,
            },
            {
                path: 'manage',
                element: <ManageLayout />,
                children: [
                    {
                        path: 'list',
                        element: <List />,
                    },
                    {
                        path: 'star',
                        element: <Star />,
                    },
                    {
                        path: 'trash',
                        element: <Trash />,
                    },
                ],
            },

            {
                path: '*', // 404配置
                element: <Notfound />,
            },
        ],
    },
    {
        path: 'question',
        element: <QuestionLayout />,
        children: [
            {
                path: 'edit/:id',
                element: <Edit />,
            },
            {
                path: 'stat/:id',
                element: <Statistics />,
            },
        ],
    },
])

export default router

// ---------- 分割线 ----------------
export const HOME_PATHNAME = '/'
export const LOGIN_PATHNAME = '/login'
export const REGISTER_PATHNAME = '/register'
export const MANAGE_INDEX_PATHNAME = '/manage/list'

export function isLoginOrRegister(pathname: string) {
    if ([LOGIN_PATHNAME, REGISTER_PATHNAME].includes(pathname)) {
        return true
    }
    return false
}

// 当前url是否不需要校验用户信息可以直接访问
export function isNoNeedUserInfo(pathname: string) {
    if ([LOGIN_PATHNAME, REGISTER_PATHNAME, HOME_PATHNAME].includes(pathname)) return true
    return false
}
